﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Data.SqlClient;

namespace 车牌识别系统1
{
    public partial class Form1 : Form
    {
        public static string a;
        public static string b;
        public static string c;
        public static string h;
        public static string k;
        public static int l;
        public static int z;
        public static string n;
        public static string m;
        public static string  i;
        public static int y;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            toolStripStatusLabel2.Text = "进入时间:" + DateTime.Now.ToLongTimeString();
            toolStripStatusLabel1.Text = "日期:" + DateTime.Now.ToShortDateString();
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;
            // p.Start();
            p.StartInfo.Arguments = "/c " + "tesseract 4.png 5 -l eng";
            p.Start();
            string output = p.StandardOutput.ReadToEnd();
            StreamReader str = new StreamReader(@"C:\Users\hp-\Desktop\刘春莹，吕昕家，苏方健\cpsb\车牌识别系统1\bin\Debug\5.txt");
            textBox1.Text = str.ReadToEnd();          
          

            string sqlStr = "server=.;database=cpsm;integrated security=SSPI";
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();
            string selectsql = string.Format("Select * from table_7  ", con);
            SqlCommand com = new SqlCommand(selectsql, con);
            com.CommandType = CommandType.Text;
            SqlDataReader sdr;
            sdr = com.ExecuteReader();
            while (sdr.Read())
            {
                a = sdr["num"].ToString().Trim();
            }
            sdr.Close();
            con.Close();
            string i;

            if (a == textBox1.Text.Trim())
            {
                textBox2.Text = textBox1.Text;

                con.Open();
                com.Connection = con;
                DateTime data = DateTime.Now;
                com.CommandText = "insert into table_8( timeb,num) values('" + data.ToString("HH:mm:ss") + "','" + textBox1.Text + "')";
                com.ExecuteNonQuery();
                con.Close();
                l = data.Hour;
                z = data.Minute;

                con.Open();
                string selectsql1 = string.Format("Select * from table_9 where num='" + textBox1.Text + "' ", con);
                SqlCommand com1 = new SqlCommand(selectsql1, con);
                com1.CommandType = CommandType.Text;
                SqlDataReader sdr1;
                sdr1 = com1.ExecuteReader();
                while (sdr1.Read())
                {
                    n = sdr1["hour"].ToString().Trim();
                    m = sdr1["minute"].ToString().Trim();
                }
                sdr1.Close();
                con.Close();

                int aa = int.Parse(n) * 60 + int.Parse(m);
                int bb = l * 60 + z;
                int cc = bb - aa;
                textBox5.Text = cc.ToString();
            }
            else
            {
                //string sqlStr = "server=.;database=cpsm;integrated security=SSPI";
                //SqlConnection con = new SqlConnection(sqlStr);
                con.Open();
                string selectsql2 = string.Format("Select * from table_2 ", con);
                SqlCommand com2 = new SqlCommand(selectsql2, con);
                com2.CommandType = CommandType.Text;
                SqlDataReader sdr2;
                sdr2 = com2.ExecuteReader();
                while (sdr2.Read())
                {
                    textBox2.Text = textBox1.Text;
                    if (textBox2.Text.Trim() == sdr2["numb"].ToString().Trim())
                    {
                        textBox3.Text = sdr2["leibie"].ToString();
                    }
                }
                sdr2.Close();
                con.Close();



                DateTime data1 = DateTime.Now;
                con.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = con;
                comm.CommandText = "insert into table_7(num)  values('" + textBox1.Text + "');insert into table_6(timea,num)  values('" + data1.ToString("HH:mm:ss") + "','" + textBox1.Text + "')";
                comm.ExecuteNonQuery();
                con.Close();
                h = data1.Hour.ToString();
                k = data1.Minute.ToString();

                con.Open();
                //SqlCommand comm = new SqlCommand();
                comm.Connection = con;
                comm.CommandText = "insert into table_9(hour,minute,num)  values('" + h + "','" + k + "','" + textBox1.Text + "')";
                comm.ExecuteNonQuery();
                con.Close();
            }
            if (string.Empty != textBox5.Text)
            {
                con.Open();
                string select1 = string.Format("Delete from table_7 where num ='" + textBox1.Text + "' ", con);
                SqlCommand comm = new SqlCommand(select1, con);
                comm.ExecuteNonQuery();
                con.Close();
            }


            if (textBox3.Text == "管理员" || textBox3.Text == "公司人员")
            {
                textBox4.Text = "0";
            }
            else
            {
                i = textBox5.Text;
                if (i == "1")
                {
                    y = 3;
                    textBox4.Text = y.ToString();
                }
                else if (i == "2")
                {
                    y = 4;
                    textBox4.Text = y.ToString();
                }
                else if (i == "3")
                {
                    y = 5;
                    textBox4.Text = y.ToString();
                }
                else if (i == "4" || i ==" 5 "|| i ==" 6" || i == "7" || i ==" 8" || i ==" 9" || i == "10" || i == "11" || i == "12")
                {
                    y = 6;
                    textBox4.Text = y.ToString();
                }
            }


        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.Show();
            this.Hide(); 
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }
    }
}
